package to4.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Inlog {
	private TO4 thema;
	private String InlogEmail;
	private String InlogPassword;
	private TextBox emailBox;
	private PasswordTextBox passwordBox;
	public Inlog(TO4 thema5){
		this.thema = thema5;
	}
	public Panel createPage() {
		FlowPanel inlogPanel = new FlowPanel();

		//createField()
		VerticalPanel inlogVertical = new VerticalPanel();
		HorizontalPanel inlogHorizon1 = new HorizontalPanel();
		HorizontalPanel inlogHorizon2 = new HorizontalPanel();
		Label emailLabel = new Label("E-mailadres:");
		emailBox = new TextBox();
		emailLabel.setStyleName("inlogLabel");
		emailBox.setVisibleLength(40);
		emailBox.setStyleName("inlogTextBox");
		passwordBox = new PasswordTextBox();
		passwordBox.setVisibleLength(40);
		passwordBox.setStyleName("passwordBox");
		Label passwordLabel = new Label("Wachtwoord:");
		passwordLabel.setStyleName("passwordLabel");
		inlogHorizon1.add(emailLabel);
		inlogHorizon1.add(emailBox);
		inlogHorizon2.add(passwordLabel);
		inlogHorizon2.add(passwordBox);
		inlogVertical.add(inlogHorizon1);
		inlogVertical.add(inlogHorizon2);

		//createButton()
		VerticalPanel buttonVertical = new VerticalPanel();
		HorizontalPanel buttonHorizon = new HorizontalPanel();
		Button inlogButton = new Button("Inloggen");
		inlogButton.addStyleName("inlogButton");
		buttonHorizon.add(inlogButton);
		buttonVertical.add(buttonHorizon);
		
		passwordBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					if(emailBox.getText().equals("") || passwordBox.getText().equals("")){
						Window.alert("U heeft een veld leeggelaten");
					}
					else {
						InlogEmail = emailBox.getText();
						InlogPassword = passwordBox.getText();
						thema.sendInlog();
					}
				}
			}
		});
		emailBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					if(emailBox.getText().equals("") || passwordBox.getText().equals("")){
						Window.alert("U heeft een veld leeggelaten");
					}
					else {
						InlogEmail = emailBox.getText();
						InlogPassword = passwordBox.getText();
						thema.sendInlog();
					}
				}
			}
		});
		
		inlogButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(emailBox.getText().equals("") || passwordBox.getText().equals("")){
					Window.alert("U heeft een veld leeggelaten");
				}
				else {
					InlogEmail = emailBox.getText();
					InlogPassword = passwordBox.getText();
					thema.sendInlog();
				}
			}
		});

		//createLink()
		VerticalPanel verticalLink = new VerticalPanel();
		HorizontalPanel horizonLink1 = new HorizontalPanel();
		HorizontalPanel horizonLink2 = new HorizontalPanel();
		Anchor wachtwoord = new Anchor("Wachtwoord vergeten?");
		horizonLink1.add(wachtwoord);
		Anchor registreren = new Anchor("Registreren");
		wachtwoord.addStyleName("link1");
		horizonLink2.add(registreren);
		registreren.addStyleName("link2");
		verticalLink.add(horizonLink1);
		verticalLink.add(horizonLink2);

		wachtwoord.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				thema.showWachtwoord();
			}
		});

		registreren.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				thema.showRegistreren();
			}
		});

		//Samenvoegen panels
		inlogPanel.add(inlogVertical);
		inlogPanel.add(buttonVertical);
		inlogPanel.add(verticalLink);
		return inlogPanel;
	}

	//Haalt waarde uit emailBox
	public String getInlogEmail(){
		return InlogEmail;
	}

	//Haalt waarde uit paswordBox
	public String getInlogPassword(){
		return InlogPassword;
	}
	
	public void setTextBoxtoNull(){
		passwordBox.setText("");
		emailBox.setText("");
	}
}